	$(function() {
		$("#tabs").tabs();

	});
	geomsa_code_id="";
var is_ward;
	function select(opt) {		
		var chk = document.getElementById("ca_name");
		var ca_name = $("select[name='ca_name']").val();

		for (var i = 0; i < chk.length; i++) {
			if (opt == chk.options[0].value) {
				document.getElementById("sanghae").style.display = "none";
				document.getElementById("samang").style.display = "none";
			} else if (opt == chk.options[1].value) {
				document.getElementById("sanghae").style.display = "";
				document.getElementById("samang").style.display = "none";
			} else if (opt == chk.options[2].value) {
				document.getElementById("sanghae").style.display = "none";
				document.getElementById("samang").style.display = "";
			}
		}			
		}
	$(document).ready(function(){

		$(".JindanseoBtn").click(function(){
			var jupsoo_id = $("input[name='jupsoo_id']").val();
  			if(addfile === null){ 
  				addfile="NOT EMPTY";
  			}
  			habitat_detail_id = $("input[name='habitat_detail_id'").val();
  			geomsa_code_id = $("select[id='geomsa_code_id']").val();
  			var fee_id =  $("select[id='fee_id']").val();
  			is_ward = $("select[name='is_ward']").val();
  			
  			opinion = $("textarea[name='opinion']").val();
			var habitat_detail_id = $("input[name='habitat_detail_id'").val();
  			geomsa_code_id = $("select[id='geomsa_code_id']").val();
  			var fee_id =  $("select[id='fee_id']").val();
  			var opinion = $("textarea[name='opinion']").val();
  			
			  $.ajax({ //처방 TEMP에 서식지id, 검사코드,접수id,의사소견 인서트
				  url : "cheobang_temp_Insert1.do", 
				  data :  'habitat_detail_id='+habitat_detail_id+'&geomsa_code_id='+geomsa_code_id+'&jupsoo_id='+jupsoo_id+'&opinion='+opinion+'&fee_id='+fee_id,
				  success:function(data){
					   $.ajax({ //처방 TEMP_ID 가져옴
						   url : "cheobang_temp_Select2.do",
						   data : 'jupsoo_id='+jupsoo_id,
						   success : function(data){
									 var cheobang_temp_id =  data.cheobang_temp[0].cheobang_temp_id;
							$.ajax({ //가져온 TEMP_ID로 처방 TABLE에 인서트
								url:"Jindanseo_cheoInsert.do",
								data:'habitat_detail_id='+habitat_detail_id+'&geomsa_code_id='+geomsa_code_id+'&cheobang_temp_id='+cheobang_temp_id+'&opinion='+opinion+'&jupsoo_id='+jupsoo_id+'&fee_id='+fee_id,
	
								success:function(data){
									
									$.ajax({
										url : "cheobang_id.do",
										data : "jupsoo_id="+jupsoo_id,
										success : function(data){
											//처방 ID를 뽑아옴
											var cheobang_id = data.cheobang.cheobang_id;
											$.ajax({
													url:"jindanseo_jupsoo_type_select.do",
												data:"jupsoo_id="+jupsoo_id,
												success : function(data){
													var type = data.type.type;
													//진단 타입이 상해(CTF01) 이면,
													if(type === 'CTF01'){
														$.ajax({
														url:"jindanseo_code_insert.do",
														data : "cheobang_id="+cheobang_id,
														success : function(data){														
															$.ajax({
																url:"injury_jindanseo_select.do",
																data : "cheobang_id="+cheobang_id,
																dataType : "json",
																success : function(data){
																var jindanseo_code_id = data.jindancodeid.jindanseo_code_id;
															
																$.ajax({
																	url:"jindanseo.do",
																	data:"jupsoo_id="+jupsoo_id,
																	success : function(data){
																		$("input[class='jindanseo_jupsoo_id']").val(data.jindan.jindanseo_jupsoo_id);
																		$("input[class='name']").val(data.jindan.target);
																		$("input[class='jumin1']").val(data.jindan.jumin1);
																		$("input[class='jumin2']").val(data.jindan.jumin2);
																		$("input[class='jupsoo_date'").val(data.jindan.jupsoo_date);
																		
																		alert("상해 진단서 Tab으로 이동하세요.");
																		
																		$(".jindan_btn").click(function(){
																			var injury_date = $("input[class='injury_date']").val();
																			$.ajax({
																				url:"injury_jindanseo_Insert.do",
																				data : "jindanseo_code_id="+jindanseo_code_id+"&injury_date="+injury_date,
																				success : function(data){
																					$("input:text").val("");
																					$("textarea").val("");
																					alert("상해 진단서 완료");
																				}
																			});
																		});
																	}
																});	
															}
														});
													}
												});
													}else if(type === 'CTF02'){											
														$.ajax({
															url:"jindanseo_code_samang_insert.do",
															data : "cheobang_id="+cheobang_id,
															success : function(data){
																$.ajax({
																	url:"injury_jindanseo_samang_select.do",
																	data : "cheobang_id="+cheobang_id,
																	dataType : "json",
																	success : function(data){
																	var jindanseo_code_id = data.jindancodeid.jindanseo_code_id;
																		 $.ajax({
																			url:"jindanseo_samang.do",
																			data:"jupsoo_id="+jupsoo_id,
																			success : function(data){
																				$("input[class='jindanseo_jupsoo_id2']").val(data.deathjindan.jindanseo_jupsoo_id);
																				$("input[class='name2']").val(data.deathjindan.target);
																				$("input[class='jumin3']").val(data.deathjindan.jumin1);
																				$("input[class='jumin4']").val(data.deathjindan.jumin2);
																				$("input[class='jupsoo_date2']").val(data.deathjindan.jupsoo_date);
																				alert("사망 진단서 Tab으로 이동하세요.");
																				$(".jindan_btn").click(function(){
																					var death_date = $("input[class='death_date']").val();
																					var death_location = $("input[class='death_location']").val();
																					
																					$.ajax({
																						url:"injury_jindanseo_samang_Insert.do",
																						data : "jindanseo_code_id="+jindanseo_code_id+"&death_date="+death_date+"&death_location="+death_location,
																						success : function(data){
																							 $("input:text").val("");
																							 $("textarea").val("");
																							alert("사망 진단서 완료");
																						}
																					});
																				});
																			}
																		 });	
																	}
																});
															}
														});
													}
												}
											});	
										}
									});
								}
							});
						   }
					   });
				  	}
			  	});
			});
		
		$("#tabs-1m").click(function() {
			$("#search_body").css("display", "block");
		});
		$("#tabs-2m").click(function() {
			$("#search_body").css("display", "none");
		});
		$("#tabs-3m").click(function() {
			$("#search_body").css("display", "none");
		});
		$("#tabs-4m").click(function() {
			$("#search_body").css("display", "none");
		});
		$("#is_ward").bind("change",function(){
			is_ward = $("#is_ward").val();
			var html = "<input type='hidden' id='is_ward_value' name='is_ward' value='"+is_ward+"' readonly>";
			$("#ward").html(html);
		});
		var idx;
		  /* waiting list <tr> 클릭 시 infoDiv에 클릭한 환자의 정보 뿌림*/
		 $(document).on("click",".selectname",function(){
				idx = $(".selectname").index(this);
				$("#infoDiv").html($(this).find("input[id='infolist']").val());	
			}); 
		 /*환자진료 진단받은 병목록, 처방 약 목록 추가기능*/
		 $(document).on("click",".ui-tabs-anchor",function() {
				var patient_id = $("input[id='patient_id']").val();
					$.ajax({
						url : "cheobang_history.do",
						data : "patient_id=" + patient_id,
						success : function(data) {

						var html = "<tr class='jindan_tr1'><th class='width100px'>상병코드</th><th>상병이름</th></tr>";
						for (var i = 0; i < data.list.length; i++) {
							html += "<tr>";
							html += "<td>"
								+ data.list[i].disease_ID
									+ "</td>";
							html += "<td>"
								+ data.list[i].disease_NAME
									+ "</td></tr>";
						}
						$(".jindan_table2").html(html);
						$.ajax({
							url : "medi_cheobang.do",
							data : "patient_id="
									+ patient_id,
							success : function(
									tata) {

								var htm = "<tr style='width:100%;'><th class='width100px'>약 코드</th><th>약 이름</th><th class='width100px'>개수</th><th class='width100px'>단위</th></tr>";
								for (var i = 0; i < tata.list2.length; i++) {
									htm += "<tr>";
									htm += "<td>"
											+ tata.list2[i].medicine_id
											+ "</td>";
									htm += "<td>"
											+ tata.list2[i].medi_name
											+ "</td>";
									htm += "<td>"
											+ tata.list2[i].quantity
											+ "</td>";
									htm += "<td>"
											+ tata.list2[i].unit
											+ "</td></tr>";
								}
								$('.chebang_table2').empty();
								$(".chebang_table2").append(htm);
							}
						});
					}
				});
			});

		 var addfile="";
		 //****진료 버튼***** 누르면 input type=text에 환자정보 출력 & 처방대기리스트에서 삭제
			$(document).on("click","#button1",function(){
				$("#infoDiv").empty();
				var patientjupsooid = $("input[id='jupsooID']").eq(idx).val();
				var delid = $("input[id='jupsooID']").eq(idx).val();
				$.ajax({
					url :'doctor2.do',
					data : 'patientjupsooid='+patientjupsooid+'&delid='+delid,
					dataType : 'json',
					success : function(data){			
	                    setTimeout(reset(),1);
	                    cheobang_temp_id=data.patientinfo.cheobang_temp_id;
	                    addfile=data.patientinfo.addfile;
	                    var is_jindanseo = data.patientinfo.is_jindanseo;
	                    patient_id = data.patientinfo.patient_id;

	                    var html = "<fieldset style='width: 673px;'>"
	                    			+"<legend>환자정보</legend>"
									+"<table>"
									+"<tr>"
									+"<td>환자번호</td>"
									+"<td>환자이름</td>"
									+"<td>생년월일</td>"
									+"<td>성별</td>"
									+"<td>접수번호</td>"
									+"<td>진단서환자여부</td>"
									+"</tr>"
									+"<tr>"
									+"<td><input type='text' id='patient_id' name='patient_id' readonly value='"+data.patientinfo.patient_id+"'></td>"
									+"<td><input type='text' id='name' name='name' readonly value='"+data.patientinfo.name+"'></td>"
									+"<td><input type='text' id='jumin' name='jumin1' readonly value='"+data.patientinfo.jumin1+"'></td>"
									+"<td><input type='text' id='gender' name='gender' readonly value='"+data.patientinfo.gender+"'></td>"
									+"<td><input type='text' id='jupsoo_id' name='jupsoo_id' readonly value='"+data.patientinfo.jupsoo_id+"'></td>"
									+"<td></td>"
									+"</tr>"
									+"</table>"
									+"</fieldset>";
                   	 var html2 = "<fieldset>"
		    				+"<legend>입원여부</legend>"
		    				+"<table>"
		   					+"<tr>"
		   					+"<td>입원여부</td>"
		   					+"</tr>"
		   					+"<tr>"
		   					+"<td><select id='is_ward'>"
			   				+"<option value='Y'>입원</option>"
			   				+"<option value='N' selected>입원 안함</option>"
			   				+"</select></td>"
			    			+"</tr>"
			    			+"</table>"
			    			+"</fieldset>";
		   					//+"<option selected value='N'>-입원여부선택-</option>"
	                    
								if(is_jindanseo === 'Y'){
									alert("진단서 접수 환자입니다.")

									//html += "<input type='text' value='진단 접수 환자' readonly>";
									$(".is_jindanseo_div").show();
									$(".cheobangBtn").hide();

									html += "<input type='text' value='진단 접수 환자' readonly>";
									$("#is_jindanseo_div").css("display", "block");

								}
								
								if(data.patientinfo.habitat_detail_id != null){
									$("select[id='geomsa_code_id']").val(data.patientinfo.geomsa_code_id);
									if(data.patientinfo.is_ward === 'N'){
				                    	 var html2 = "<fieldset>"
				 		    				+"<legend>입원여부</legend>"
				 		    				+"<table>"
				 		   					+"<tr>"
				 		   					+"<td>입원여부</td>"
				 		   					+"</tr>"
				 		   					+"<tr>"
				 		   					+"<td><select id='is_ward'>"
							   				+"<option value='Y'>입원</option>"
							   				+"<option value='N' selected>입원 안함</option>"
							   				+"</select></td>"
							    			+"</tr>"
							    			+"</table>"
							    			+"</fieldset>";
				                    }else{
				                    	 var html2 = "<fieldset>"
					 		    			+"<legend>입원여부</legend>"
					 		    			+"<table>"
					 		   				+"<tr>"
					 		   				+"<td>입원여부</td>"
					 		   				+"</tr>"
					 		   				+"<tr>"
					 		   				+"<td><select id='is_ward'>"
					 		   				+"<option value='Y' selected>입원</option>"
					 		   				+"<option value='N'>입원 안함</option>"
					 		   				+"</select></td>"
					 		   				+"</tr>"
					    					+"</table>"
					    					+"</fieldset>";
				                    }

									/*"<div class='habitat_detail_id'>"*/
									html3 = "<fieldset>"
									+"<legend>서식지</legend>"
									+"<table>"
									+"<tr>"
									+"<td>서식지 선택</td>"
									+"<td>대분류</td>"
									+"<td>소분류</td>"
									+"</tr>"
									+"<tr>"
									+"<td><button type='button' id='seosikbtn'>선택</button>" 
									+"<input type='hidden' class='num' name='habitat_detail_id' value='"+data.patientinfo.habitat_detail_id+"' readonly></td>"
									+"<td><input type='text' class='dae' name='type' readonly value='"+data.patientinfo.type+"' readonly></td>"
									+"<td><input type='text' class='so' name='content' readonly value='"+data.patientinfo.content+"'></td>"
									+"</tr>"
									+"</table>"
									+"</fieldset>";
							
								}else{
								 geomsa_code_id = $("select[id='geomsa_code_id']").val();
								 html3 = "<fieldset>"
										+"<legend>서식지</legend>"
										+"<table>"
										+"<tr>"
										+"<td>서식지 선택</td>"
										+"<td>대분류</td>"
										+"<td>소분류</td>"
										+"</tr>"
										+"<tr>"
										+"<td><button type='button' id='seosikbtn'>선택</button>" 
										+"<input type='hidden' class='num' name='habitat_detail_id' readonly></td>"
										+"<td><input type='text' class='dae' name='type' readonly></td>"
										+"<td><input type='text' class='so' name='content' readonly></td>"
										+"</tr>"
										+"</table>"
										+"</fieldset>";
								}
						 $("#patientinfo").html(html);
						 $("#is_ward_div").html(html2);
						 $('.habitat_detail_id').html(html3);
					}
				});  
		 	}); 
			
			/* 서식지 선택버튼 눌렀을때 리스트 뿌려줌 */ 
		$(document).on("click", "#seosikbtn", function(){
			$("#seosik_div").css("display", "block");
			$("#disease_div").css("display", "none");
			$("#medicine_div").css("display", "none");
			
			$.ajax({
				url :'seosik.do',
				dataType : 'json',
				success : function(data){
					$('.ptr').empty();
					$(".ptr").append("<tr><th class='seosikTh1'>대분류</th><th>소분류</th></tr>");
						for(var i =0; i<data.seosik.length;i++){
							$(".ptr").append("<tr class='seosikTr'><td><input type='hidden' name='hidden1' value='"+data.seosik[i].type+"'>"+data.seosik[i].type+"</td>"+
							 "<td><input type='hidden' name='hidden2' value='"+data.seosik[i].content+"' readonly>"+data.seosik[i].content+"</td>"+
							 "<td><input type='hidden' class='num' name='hidden3' value='"+data.seosik[i].habitat_detail_id+"'></td></tr>");
						}
						$('.seosikTr').mouseenter(function() {
								$(this).css('background-color','#F9F9F9');
								$(this).css('cursor','pointer');
						});
						$('.seosikTr').mouseleave(function() {
							$(this).css('background-color','#ffffff');
						});
					}
				});
			});
					
			/* 서식지 검색 리스트 뿌려줌 */ 
			$(document).on("click", "#seosik_searchbtn", function(){
					var seosiksearch = $("#inputtag").val();
					$.ajax({
						url: "seosiksearch.do",
						data : "seosiksearch="+seosiksearch,
						dataType : 'json',
						success : function(data){
							$('.ptr').empty();
							$(".ptr").append("<tr><th class='seosikTh1'>대분류</th><th>소분류</th></tr>");
							 for(var i = 0; i<data.seosiksearch.length;i++){
								 $(".ptr").append("<tr class='seosikTr'><td><input type='hidden' name='hidden1' value='"+data.seosiksearch[i].type+"'>"+data.seosiksearch[i].type+"</td>"+
										 "<td><input type='hidden' name='hidden2' value='"+data.seosiksearch[i].content+"'>"+data.seosiksearch[i].content+"</td><input type='hidden' class='hidden3' name='hidden3' value='"+data.seosiksearch[i].habitat_detail_id+"'></tr>");
								 }
							$('.seosikTr').mouseenter(function() {
									$(this).css('background-color','#F9F9F9');
									$(this).css('cursor','pointer');
							});
							$('.seosikTr').mouseleave(function() {
								$(this).css('background-color','#ffffff');
							});
						}
					});
				});
			
			/* 서식지 tr 클릭 시 그 값 input text에 출력*/
			 $(document).on("click", ".seosikTr", function(){
					var idx1 = $(".seosikTr").index(this);
					var seosikTrclick1 = $("input[name='hidden1']").eq(idx1).val();
					var seosikTrclick2 = $("input[name='hidden2']").eq(idx1).val();
					var seosikTrclick3 = $("input[name='hidden3']").eq(idx1).val();

					$("input[class='dae']").val(seosikTrclick1);
					$("input[class='so']").val(seosikTrclick2);
					$("input[name='habitat_detail_id']").val(seosikTrclick3);
				 });
		
			 /* 상병 선택 눌렀을때 리스트 뿌려줌 */ 
			$(document).on("click", "#diseasebtn", function(){
				$("#disease_div").css("display", "block");
				$("#seosik_div").css("display", "none");
				$("#medicine_div").css("display", "none");
				$("#jusa_div").css("display", "none");
				habitat_detail_id = $("input[name='habitat_detail_id'").val();
				var jupsoo_id = $("input[name='jupsoo_id']").val();
					if(cheobang_temp_id === null){
						//alert("들어옴");
						$.ajax({
							url :'tempinsert.do', 
							data:'habitat_detail_id='+habitat_detail_id+'&jupsoo_id='+jupsoo_id,
							dataType : 'json',
							success : function(data){
								cheobang_temp_id = data;
							},
								error:function(data){
								    alert("서식지를 선택해주세요.");
								}
							});
						}
					
						$.ajax({
								url :'disease.do',
								dataType : 'json',
								success : function(data){
									$('.ptr').empty();					
									$(".ptr").append("<tr><th class='diseaseTh1'>상병코드</th><th>상병명</th></tr>");
										for(var i =0; i<data.disease.length;i++){
											$(".ptr").append("<tr class='diseaseTr'><td><input type='hidden' name='hidden3' value='"+data.disease[i].disease_id+"'>"+data.disease[i].disease_id+"</td>"+
											 "<td><input type='hidden' name='hidden4' value='"+data.disease[i].name+"'>"+data.disease[i].name+"</td></tr>");
										 }
										
										$('.diseaseTr').mouseenter(function() {
											$(this).css('background-color','#F9F9F9');
											$(this).css('cursor','pointer');
										});
										$('.diseaseTr').mouseleave(function() {
											$(this).css('background-color','#ffffff');
										});
										
									}
						
								});  
						
							});
			
				/* 상병 검색  */ 
				$(document).on("click", "#diseasesearchbtn", function(){
					var diseasesearch = $("#inputtag2").val();
						$.ajax({
							url: "diseasesearch.do",
							data : "diseasesearch="+diseasesearch,
							dataType : 'json',
							success : function(data){
								$('.ptr').empty();
								$(".ptr").append("<tr><th class='diseaseTh1'>상병코드</th><th>상병명</th></tr>");

								for(var i =0; i<data.diseasesearch.length;i++){
									 $(".ptr").append("<tr class='diseaseTr'><td><input type='hidden' name='hidden3' value='"+data.diseasesearch[i].disease_id+"'>"+data.diseasesearch[i].disease_id+"</td>"+
											 "<td><input type='hidden' name='hidden4' value='"+data.diseasesearch[i].name+"'>"+data.diseasesearch[i].name+"</td></tr>");
									 }
								$('.diseaseTr').mouseenter(function() {
										$(this).css('background-color','#F9F9F9');
										$(this).css('cursor','pointer');
								});
									$('.diseaseTr').mouseleave(function() {
										$(this).css('background-color','#ffffff');
								});
							}
						});
					});		
						
			/*  상병 tr 클릭 시 그 값 input text에 출력*/	
	 		$(document).on("click", ".diseaseTr",function(){
	 			var idx2 = $(".diseaseTr").index(this);
	 			var diseaseTrclick1 = $("input[name='hidden3']").eq(idx2).val();
	 			var diseaseTrclick2 = $("input[name='hidden4']").eq(idx2).val();
						
	 			$("input[class='diseasecode']").val(diseaseTrclick1);
	 			$("input[class='diseasename']").val(diseaseTrclick2);
	 		});	
	 		
	 		/*  상병 추가버튼 클릭    */
			$(document).on("click", ".diseaseaddBtn",function(){
				
				if($('input:text[name=patient_id]').val() === ""){
					alert("환자를 선택해주세요");
				}else if($('input:text[class=diseasecode]').val() == ""){
						alert('상병을 선택해주세요');
				}else{
					
					var result = confirm('정말로 추가하시겠습니까?');

			        if(result) {
			        	var diseasecode = $("input[class='diseasecode']").val();
						var diseasename = $("input[class='diseasename']").val();	
						html="";
						html+="<tr><td>"+diseasecode+"</td>";
						html+="<td>"+diseasename+"</td>";
						$(".jindan_tr1").after(html);		
						var jupsoo_id = $("input[name='jupsoo_id']").val();
						   $.ajax ({
						     url : "cheobangselect1.do",
						     data : 'jupsoo_id='+jupsoo_id,
						     success : function(data){
							   	
						    	 var cheobang_temp =  data.testmove2.cheobang_temp_id;
						 		$.ajax({
						 			url: "cheobanginsert.do",
									data : "cheobang_temp="+cheobang_temp+"&diseasecode="+diseasecode,
									success: function(data){
										
									}
								});
						     }
						   
						   });
						   
			        	} else {
			        		
			            return false;
			            
				        }
					}
				});			
	 		
				/* 약 선택 눌렀을때 리스트 뿌려줌 */ 
		$(document).on("click", "#medicinebtn", function(){
			$("#medicine_div").css("display", "block");
			$("#disease_div").css("display", "none");
			$("#seosik_div").css("display", "none");
				$.ajax({
					url :'medicine.do',
					dataType : 'json',
					success : function(data){
						$('.ptr').empty();
						$(".ptr").append("<tr><th>약 코드</th><th>약 이름</th><th>단위</th></tr>");
						 for(var i =0; i<data.medicine.length;i++){
							 $(".ptr").append("<tr class='mediTr'><td><input name='hidden5' type='hidden' value='"+data.medicine[i].medicine_id+"'>"+data.medicine[i].medicine_id+"</td>"+
								 		"<td><input name='hidden6' type='hidden' value='"+data.medicine[i].name+"'>"+data.medicine[i].name+"</td>"+
										"<td><input name='hidden8' type='hidden' value='"+data.medicine[i].unit+"'>"+data.medicine[i].unit+"</td></tr>");
						 }
						 $('.mediTr').mouseenter(function() {
								$(this).css('background-color','#F9F9F9');
								$(this).css('cursor','pointer');
						});
							$('.mediTr').mouseleave(function() {
								$(this).css('background-color','#ffffff');
						});
					}
				});
			});
				/* ----------- 약 검색-----------*/
		$(document).on("click", "#medi_search", function(){
			var medisearch = $("#inputtag3").val();
				$.ajax({
					url: "medisearch.do",
					data : "medisearch="+medisearch,
					dataType : 'json',
					success : function(data){
						$('.ptr').empty();
						$(".ptr").append("<tr><th>약 코드</th><th>약 이름</th><th>단위</th</tr>");

						 for(var i =0; i<data.medisearch.length;i++){
							 $(".ptr").append("<tr class='mediTr'><td><input name='hidden5' type='hidden' value='"+data.medisearch[i].medicine_id+"'>"+data.medisearch[i].medicine_id+"</td>"+
						 		"<td><input name='hidden6' type='hidden' value='"+data.medisearch[i].name+"'>"+data.medisearch[i].name+"</td>"+
								"<td><input name='hidden8' type='hidden' value='"+data.medisearch[i].unit+"'>"+data.medisearch[i].unit+"</td></tr>");
							 }
						 
						 $('.mediTr').mouseenter(function() {
								$(this).css('background-color','#F9F9F9');
								$(this).css('cursor','pointer');
						});
							$('.mediTr').mouseleave(function() {
								$(this).css('background-color','#ffffff');
						});
					}
				});
			});
				
		/*  약 tr 클릭 시 그 값 input text에 출력*/
 		$(document).on("click", ".mediTr",function(){
		var idx3 = $(".mediTr").index(this);
		var mediTrclick1 = $("input[name='hidden5']").eq(idx3).val();
		var mediTrclick2 = $("input[name='hidden6']").eq(idx3).val();
		var mediTrclick4 = $("input[name='hidden8']").eq(idx3).val();

		$("input[class='medicode']").val(mediTrclick1);
		$("input[class='mediname']").val(mediTrclick2);
		$("input[class='mediunit']").val(mediTrclick4);
	 });   
				
 		/*         약 추가 버튼 클릭 시 밑의 처방 약 목록에 뿌려줌        */
		$(document).on("click", ".medicineAddBtn",function(){
			if($('input:text[name=patient_id]').val() === ""){
				alert("환자를 선택해주세요");
			}else if($('input:text[class=medicode]').val() === ""){
				alert('처방약을 선택해주세요')
			}else if($('input:text[class=mediamount]').val() == ""){
	        	alert('약 갯수를 지정해주세요');
			}else{
				var result = confirm('정말로 추가하시겠습니까?');
		        if(result) {
		        	var medicode = $("input[class='medicode']").val();
					var mediname = $("input[class='mediname']").val();
					var mediamount = $("input[class='mediamount']").val();
					var mediunit = $("input[class='mediunit']").val();
						$.ajax({
							url: "mediaddlist.medic",
							data : "medicode="+medicode+"&mediamount="+mediamount,
							dataType : 'json',
							success : function(data){
								if(data === "success"){
									
								}
							}
						});
						
						html="";
						html+="<tr><td class='cheobang1'>"+medicode+"</td>";
						html+="<td class='cheobang2'>"+mediname+"</td>";
						html+="<td class='cheobang3'>"+mediamount+"</td>";
						html+="<td class='cheobang4'>"+mediunit+"</td></tr>";
						$(".zzz").after(html);		
						
						$(".mediamount").empty();
						$(".mediname").empty();
						$(".medicode").empty();
						$(".mediunit").empty();
						
		        } else {
		        	
		            return false;
		            
		        }
				
			}
		});
 		
		  //삭제 버튼 클릭 시 선택된 환자를 대기리스트에서 삭제
		  $(document).on("click",".delbutton", function(){
			  var delid;
			  if(idx != null){
				  delid = $("input[id='jupsooID']").eq(idx).val();
			  }
				$.ajax({
					url :'waitlistdel.do',
					data : 'delid='+delid,
					success : function(data){
		                    setTimeout(reset(),1);
					}
				});
		  });
		
		  reset = function(){
			  idx = null;
			  $.ajax({
				  url: 'rewaitlist.do',
				  dataType : 'json',
				  success : function(data){
					  $('.watinglist_table').empty();
					  $(".watinglist_table").append("<tr><th>환자명</th><th>접수시간</th></tr>");
					  for(var i =0; i<data.rewaitlist.length;i++){
						  $(".watinglist_table").append( "<tr class='selectname'>"+
								  "<td><input class='textval' id='jupsooID' name='jupsoo_id' type='hidden'value='"+data.rewaitlist[i].jupsoo_id+"' readonly>"+
								  "<input class='textval' type='hidden' id='patientName'value='"+data.rewaitlist[i].name +"' readonly>"+data.rewaitlist[i].name +"</td>"+
								  "<td><input class='textval' type='hidden' id='jupsooTime' value='"+data.rewaitlist[i].jupsoo_time+"' readonly>"+data.rewaitlist[i].jupsoo_time+
								  "<input id='patientid' name='patientid' style='display: none;' type='hidden' value='"+data.rewaitlist[i].patient_id+"' readonly></td></tr>");
					  }
				  }
			  });				
		  }
		  
		  var cheobang_temp_id="";
		  /* -------------- 검사실 이동 버튼 액션 ----------------- */
		  $(".TestmoveBtn").click(function() {
			var habitat_detail_id = $("input[name='habitat_detail_id'").val();
			var jupsoo_id = $("input[name='jupsoo_id']").val();
  			geomsa_code_id = $("select[id='geomsa_code_id']").val();
  			var fee_id =  $("select[id='fee_id']").val();
  			is_ward = $("select[id='is_ward']").val();
  			var opinion = $("textarea[name='opinion']").val();
  			//alert(is_ward);
			  $.ajax({
				  url : "testmove.do",
				  data :  'habitat_detail_id='+habitat_detail_id+'&geomsa_code_id='+geomsa_code_id+'&is_ward='+is_ward+'&jupsoo_id='+jupsoo_id+'&opinion='+opinion+'&fee_id='+fee_id,
				  success:function(data){
					  $.ajax({
						   url : "testmove2.do",
						   data : 'jupsoo_id='+jupsoo_id,
						   success : function(data){
									 var cheobang_temp =  data.testmove2.cheobang_temp_id;
									 $("input[name='cheobang_temp_id']").val(cheobang_temp);
							 cheobang_temp_id = $("input[name='cheobang_temp_id'").val();
							 
							 	$.ajax({
							 		url : "testmove3.do",
							 		data : "cheobang_temp_id="+cheobang_temp_id+'&fee_id='+fee_id+'&geomsa_code_id='+geomsa_code_id,
							 		success : function(data){
							 			var doctor_id = $("#doctor_id").attr('value');
							 			var name = $("#name").attr('value');
							 			$("input:text").val("");
							 			$("#doctor_id").val(doctor_id);
							 			$("#name").val(name);
							 			reset();
								 }
							 });
					    }
				   })
			   }		   
			  });
			  
			 });
		  
		  /* ---------------------- 처방 버튼 액션 ------------------ */
				$(".cheobangBtn").on("click", function(){
		  			var jupsoo_id = $("input[name='jupsoo_id']").val();
		  			var dose_id = $("#Dose_id").val();
		  			var Dose_id = $("#Dose_id option:selected").val();
		  			var opinion = $('textarea[id=opinion]').val();
		  			var dae = $('input:text[class=dae]').val();
		  				
		  				if(dae == ""){
		  					alert('서식지를 선택해주세요');
		  					return false;
		  				}
		  				if(Dose_id == "-복용법 선택-"){
		  					alert('복용법을 선택해주세요');
		  					return false;
		  				}
		  				if(opinion == ""){
		  					alert('소견을 작성해주세요');
		  					return false;
		  				}
		  			
		  				if(addfile === null){ 
			  				addfile="NOT EMPTY";
			  			}
		  				
		  				var result = confirm('처방을 완료하시겠습니까?');

		  			    if(result) {
		  			    	habitat_detail_id = $("input[name='habitat_detail_id'").val();
				  			geomsa_code_id = $("select[id='geomsa_code_id']").val();
				  			var fee_id =  $("select[id='fee_id']").val();
				  			is_ward = $("select[id='is_ward']").val();
				  			opinion = $("textarea[name='opinion']").val();
				  			var cheobang ="";
				  			cheobang +="<input type='hidden' name='geomsa_code_id' value='"+geomsa_code_id+"'>";
				  			cheobang +="<input type='hidden' name='fee_id' value='"+fee_id+"'>"; 
				  			cheobang +="<input type='hidden' name='opinion' value='"+opinion+"'>";
				  			cheobang +="<input type='hidden' name='is_ward' value='"+is_ward+"'>";
				  			cheobang +="<input type='hidden' name='habitat_detail_id' value='"+habitat_detail_id+"'>";
				  			cheobang +="<input type='hidden' name='jupsoo_id' value='"+jupsoo_id+"'>";
				  			cheobang +="<input type='hidden' name='cheobang_temp_id' value='"+cheobang_temp_id+"'>";
				  			cheobang +="<input type='hidden' name='patient_id' value='"+patient_id+"'>";
				  			cheobang +="<input type='hidden' name='addfile' value='"+addfile+"'>";
				  			cheobang +="<input type='hidden' name='dose_id' value='"+dose_id+"'>";
				  			$(".cheobanginsert").append(cheobang);
				  			$(".cheobanginsert").submit();
				  			
				  			alert("처방이 완료되었습니다");
		  					   
		  			    	} else {
		  			    		
		  			        return false;
		  			        
		  			   }
				});
			});
	
			
			
